package imachine.test.groupcall;

import imachine.Machine;
import imachine.State;
import imachine.Transition;
import imachine.Trigger;

public class ConnectedMachine extends Machine {
	public static final int ID = 11;
	public static final String name = "ConnectedFSM";

	public static final int IDLE_STATE = 0;
	public static final int LISTENING_STATE = 1;
	public static final int REQUESTING_STATE = 2;
	public static final int TALKING_STATE = 3;
	public static final int RELEASING_STATE = 4;
	
	public static final int IDLE_TO_REQUESTING_TRAN = 0;
	public static final int IDLE_TO_LISTENING_TRAN = 1;
	public static final int LISTENING_TO_IDLE_TRAN = 2;
	public static final int REQUESTING_TO_IDLE_TRAN = 3;
	public static final int REQUESTING_TO_TALKING_TRAN = 4;
	public static final int TALKING_TO_RELEASING_TRAN = 5;
	public static final int RELEASING_TO_IDLE_TRAN = 6;

	public static final int LAUNCH_CALL_EVENT = 0;
	public static final int INCOMING_CALL_EVENT = 1;
	public static final int REQUESTING_CONFIRMED_EVENT = 2;
	public static final int IDLE_EVENT = 3;
	public static final int END_CALL_EVENT = 4;

	public static final int ENTRY_STATE = IDLE_STATE;
	
	public static ConnectedMachine getInstance() {
		if (instance == null) {
			instance = new ConnectedMachine();
		}
		return instance;
	}

	
	private ConnectedMachine() {
		super();
		
		states.add(new State(IDLE_STATE));
		states.add(new State(LISTENING_STATE));
		states.add(new State(REQUESTING_STATE));
		states.add(new State(TALKING_STATE));
		states.add(new State(RELEASING_STATE));

		transitions.add(new Transition(IDLE_TO_REQUESTING_TRAN,
				IDLE_STATE,
				REQUESTING_STATE));
		transitions.add(new Transition(IDLE_TO_LISTENING_TRAN,
				IDLE_STATE,
				LISTENING_STATE));
		transitions.add(new Transition(LISTENING_TO_IDLE_TRAN,
				LISTENING_STATE,
				IDLE_STATE));
		transitions.add(new Transition(REQUESTING_TO_IDLE_TRAN,
				REQUESTING_STATE,
				IDLE_STATE));
		transitions.add(new Transition(REQUESTING_TO_TALKING_TRAN,
				REQUESTING_STATE,
				TALKING_STATE));
		transitions.add(new Transition(TALKING_TO_RELEASING_TRAN,
				TALKING_STATE,
				RELEASING_STATE));
		transitions.add(new Transition(RELEASING_TO_IDLE_TRAN,
				RELEASING_STATE,
				IDLE_STATE));

		triggers.add(new Trigger(LAUNCH_CALL_EVENT, 
				IDLE_TO_REQUESTING_TRAN));
		triggers.add(new Trigger(INCOMING_CALL_EVENT, 
				IDLE_TO_LISTENING_TRAN));
		triggers.add(new Trigger(IDLE_EVENT, 
				LISTENING_TO_IDLE_TRAN));
		triggers.add(new Trigger(REQUESTING_CONFIRMED_EVENT, 
				REQUESTING_TO_TALKING_TRAN));
		triggers.add(new Trigger(END_CALL_EVENT, 
				REQUESTING_TO_IDLE_TRAN));
		triggers.add(new Trigger(END_CALL_EVENT, 
				TALKING_TO_RELEASING_TRAN));
		triggers.add(new Trigger(IDLE_EVENT, 
				RELEASING_TO_IDLE_TRAN));
	}
	
	public String getName() {
		return name;
	}

	public int getId() {
		return ID;
	}

	public int getEntryStateId() {
		return ENTRY_STATE;
	}
	
	private static ConnectedMachine instance = null;

}
